public class ThreadDemo5 {
    public static int flag1 = 0;
    volatile public static int flag2 = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while(flag1 == 0) {

            }
            System.out.println("t1结束");
        });

        Thread t2 = new Thread(() -> {
            while(flag2 == 0) {

            }
            System.out.println("t2结束");
        });

        t1.start();
        t2.start();

        Thread.sleep(2000);

        System.out.println("设置flag1 = 1");
        System.out.println("设置flag2 = 1");
        flag1 = flag2 = 1;
    }
}
